//
//  TCM_HomeViewController.swift
//  TCM_TradingProject
//
//  Created by 吕春海 on 2018/8/17.
//  Copyright © 2018年 lvchunhai. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class TCM_HomeViewController: TCM_BaseViewController,UITableViewDelegate,UITableViewDataSource,CycleViewDelegate {

    var homeTableView:UITableView!
    let entryNameArry:[String] = ["申请入库", "我的货物", "发布供应", "发布求购", "发起拍卖"]
    let entryImageArry:[String] = ["TCM_HomeApplyPut", "TCM_HomeMyGoods", "TCM_HomeReleaseSupply", "TCM_HomeReleaseBuy", "TCM_HomeLaunchAuction"]
    let auctionArry = [["三七", "当归", "黄芪", "铁皮石斛"], ["宁夏中卫", "陕西山阳", "黑龙江齐齐", "西藏日喀则"], ["1000", "230", "100030", "78"], ["1364", "326", "150000", "127"], ["炉白皮统", "家种三等", "家种二等", "松统"], ["264", "62", "5643", "125565"]]
    let informationArry = [["市场盘点（第三期）：香料收付失地", "专家传最近西洋参可能会大幅度涨价西洋参大幅度涨价，你怎么看？", "安国市场黄精行情在坚挺中运行，有效缓解中药市场。"], ["市场盘点", "本地资讯", "药品分析"], ["刚刚", "30分钟前", "3小时前"], ["500", "1882", "2k"], ["huangjing.jpg", "huangjing.jpg", "huangjing.jpg"]]
    var isImage = true
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initialUI()
        // Do any additional setup after loading the view.
    }

    func initialUI() {
        initialNav()
        initialTableView()
    }
    
    func initialNav() {
        self.navigationItem.title = ""
        self.tabBarItem.title = "首页"
        
        let searchButton = UIButton(frame:CGRect(x: 0, y: 0, width: TCM_ScreenWidth - 96, height: 32))
        searchButton.backgroundColor = UIColor.white
        searchButton.layer.cornerRadius = 4
        searchButton.clipsToBounds = true
        
        let searchImageView = UIImageView(frame: CGRect(x: 12, y: 9, width: 16, height: 16))
        searchImageView.image = UIImage.init(named: "TCM_HomeSeach")
        searchButton.addSubview(searchImageView)
        
        let seachLabel = UILabel.init(frame: CGRect(x: 34, y: 10, width: 98, height: 14))
        seachLabel.text = "请输入药材名"
        seachLabel.font(16)
        seachLabel.textColor = TCM_HexRGB(0xB2B2B2)
        searchButton.addSubview(seachLabel)
        
        self.navigationItem.titleView = searchButton
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "TCM_Home_scan"), style: .plain, target: self, action: #selector(popViewController))
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "TCM_Home_Message"), style: .plain, target: self, action: #selector(messageButtonClick))
    }
    
    func initialTableView() {
        self.homeTableView = UITableView(frame: self.view.frame, style:.grouped)
        self.homeTableView.backgroundColor = TCM_HexRGB(0xF0F0F0)
        self.homeTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        self.homeTableView.showsVerticalScrollIndicator = false
        self.homeTableView.delegate = self
        self.homeTableView.dataSource = self
        
        self.view.addSubview(homeTableView)
        
        let cycleView : CycleView = CycleView(frame: CGRect(x: 0, y: TCM_NavBarHeight(), width: TCM_ScreenWidth, height: 210))
        cycleView.delegate = self
        cycleView.mode = .scaleAspectFill
        //本地图片测试--加载网络图片,请用第三方库如SDWebImage等
        cycleView.imageURLStringArr = ["banner01.jpg", "banner02.jpg", "banner03.jpg", "banner04.jpg", "banner05.jpg"]
        homeTableView.tableHeaderView = cycleView
                
        self.homeTableView.register(UINib.init(nibName: "TCM_HomeEntryTableViewCell", bundle: nil), forCellReuseIdentifier: "quickEntryCell")
        self.homeTableView.register(UINib.init(nibName: "TCM_HomeAnnouncementTableViewCell", bundle: nil), forCellReuseIdentifier: "AnnouncementCell")
        self.homeTableView.register(UINib.init(nibName: "TCM_HomeAuctionTirleTableViewCell", bundle: nil), forCellReuseIdentifier: "AuctionTitleCell")
        self.homeTableView.register(UINib.init(nibName: "TCM_HomeAuctionTableViewCell", bundle: nil), forCellReuseIdentifier: "AuctionCell")
        self.homeTableView.register(UINib.init(nibName: "TCM_HomeInformationTableViewCell", bundle: nil), forCellReuseIdentifier: "InformationCell")
    }
    
    // MARK: -tableview代理
    //设置cell的数量
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 2 {
            return 5
        } else if section == 3 {
            return 3
        } else {
            return 1
        }
    }
    
    //设置section的数量
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 110
        } else if indexPath.section == 1 {
            return 56
        }  else if indexPath.section == 2 {
            if indexPath.row == 0 {
                return 40
            } else {
                return 48
            }
        } else {
            //不确定
            return 108
        }
    }
    
    //设置tableview的cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.section == 0 {
            let cell = (homeTableView.dequeueReusableCell(withIdentifier: "quickEntryCell", for: indexPath)) as! TCM_HomeEntryTableViewCell
            cell.entryNameArr = self.entryNameArry
            cell.entryImageArr = self.entryImageArry
            return cell
        } else if indexPath.section == 1 {
            let cell = (homeTableView.dequeueReusableCell(withIdentifier: "AnnouncementCell", for: indexPath)) as! TCM_HomeAnnouncementTableViewCell
            cell.selectionStyle = .none
            return cell
            
        } else if indexPath.section == 2 {
            if indexPath.row == 0 {
                let cell = (homeTableView.dequeueReusableCell(withIdentifier: "AuctionTitleCell", for: indexPath)) as! TCM_HomeAuctionTirleTableViewCell
                cell.selectionStyle = .none
                return cell
            } else {
                let cell = (homeTableView.dequeueReusableCell(withIdentifier: "AuctionCell", for: indexPath)) as! TCM_HomeAuctionTableViewCell
                cell.selectionStyle = .none
                cell.nameLabel.text = auctionArry[0][indexPath.row - 1]
                cell.addressLabel.text = auctionArry[1][indexPath.row - 1]
                cell.clapLable.text = auctionArry[2][indexPath.row - 1]
                cell.latestLabel.text = auctionArry[3][indexPath.row - 1]
                cell.varietiesLabel.text = auctionArry[4][indexPath.row - 1]
                cell.numberLabel.text = auctionArry[5][indexPath.row - 1]
                
                if indexPath.row % 2 != 0 {
                    cell.contentView.backgroundColor = TCM_HexRGB(0xFBF7F5)
                    cell.nameView.backgroundColor = TCM_HexRGB(0xFBF7F5)
                    cell.clapView.backgroundColor = TCM_HexRGB(0xFBF7F5)
                    cell.latestView.backgroundColor = TCM_HexRGB(0xFBF7F5)
                    cell.varietiesView.backgroundColor = TCM_HexRGB(0xFBF7F5)
                    cell.numberView.backgroundColor = TCM_HexRGB(0xFBF7F5)
                } else {
                    cell.contentView.backgroundColor = UIColor.white
                    cell.nameView.backgroundColor = UIColor.white
                    cell.clapView.backgroundColor = UIColor.white
                    cell.latestView.backgroundColor = UIColor.white
                    cell.varietiesView.backgroundColor = UIColor.white
                    cell.numberView.backgroundColor = UIColor.white
                }
                
                return cell
            }
        } else {
            let cell = (homeTableView.dequeueReusableCell(withIdentifier: "InformationCell", for: indexPath)) as! TCM_HomeInformationTableViewCell
            cell.selectionStyle = .none

            cell.contentLabel.text = informationArry[0][indexPath.row]
            cell.typeLabel.text = informationArry[1][indexPath.row]
            cell.timeLabel.text = informationArry[2][indexPath.row]
            cell.readNumberLabel.text = informationArry[3][indexPath.row]
            
//            if indexPath.row == 2 {
                cell.contentImageView.isHidden = false
                cell.contentImageView.image = UIImage.init(named: informationArry[4][indexPath.row])
                cell.contentLabelToViewtrailing.constant = 152
//            } else {
//                cell.contentImageView.isHidden = true
//                cell.contentLabelToViewtrailing.constant = 16
//            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 3 {
            let detailsVC = TCM_InformationDetailsViewController()
            self.navigationController?.pushViewController(detailsVC, animated: true)
        }
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 2 || section == 3{
            return 40
        } else {
            return 0.00001
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 2 {
            let headerView = Bundle.main.loadNibNamed("TCM_HomeSectionHeaderView",owner: self, options: nil)?[0] as! TCM_HomeSectionHeaderView
            headerView.titleLabel.text = "正在拍卖"
            return headerView
            
        } else if section == 3 {
            let headerView = Bundle.main.loadNibNamed("TCM_HomeSectionHeaderView",owner: self, options: nil)?[0] as! TCM_HomeSectionHeaderView
            headerView.titleLabel.text = "最新资讯"
            return headerView
        }
        return nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 8
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView()
        footerView.frame = CGRect(x: 0, y: 0, width: TCM_ScreenWidth, height: 8)
        footerView.backgroundColor = TCM_HexRGB(0xF0F0F0)
        return footerView
    }
    
    // MARK: --cycleviewDelegate
    func cycleViewDidSelectedItemAtIndex(_ index: NSInteger) {
        print("点击了轮播图第\(index + 1)个图片")
//        let demoVc = DemoController()
//        demoVc.title = "点击了轮播图第\(index)个图片"
//        demoVc.view.backgroundColor = UIColor.white
//        navigationController?.pushViewController(demoVc, animated: true)
    }
    
    // MARK: --buttonClick
    @objc func messageButtonClick() {
        let messageVC = TCM_HomeSystemMessageViewController()
        self.navigationController?.pushViewController(messageVC, animated: true)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
